<style lang="scss">
  .timer_form1{
    .el-form-item__label{
      width: 120px !important
    }
    .el-form-item__content{
      margin-left:120px !important;
    }
  }


</style>
<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="workData"
               ref="crud"
               v-model="form"
               :permission="permissionList"
               :before-open="beforeOpen"
               @row-del="rowDel"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="success"
                   size="small"
                   plain
                   @click="handleImport">默认时间设置
        </el-button>
        <el-button type="info"
                   size="small"
                   plain
                   icon="el-icon-upload2"
                   @click="allUse">批量应用
        </el-button>
      </template>
      <template slot-scope="{row}"
                slot="fullStatus">
        <el-tag  type="success" v-if="+row.fullStatus === 1">正常</el-tag>
        <el-tag  type="danger" v-else-if="+row.healthy === 2">满溢</el-tag>
        <el-tag   type="info" v-else>无数据</el-tag>
      </template>
      <template slot-scope="scope" slot="menu">
        <el-button type="success"
                   size="small"
                   plain
                   @click="handleImport(scope.row)">时间更改
        </el-button>
      </template>
    </avue-crud>
    <el-dialog title="定时定点时间设置"
    append-to-body
    :visible.sync="excelBox"
    width="800px">
      <timerset  :timerRow="timerRow" v-if="excelBox" :scopeRow="scopeRow" @refresh="refresh"></timerset>
    </el-dialog>
  </basic-container>
</template>

<script>
  import {getDetail, remove,add,update,batchExecute} from "@/api/device/timer";
  import {mapGetters} from "vuex";
  import  timerset from './timerset.vue'
  export default {
    name: 'innerbarrel',
    props: ["timerRow", "timerBox"],
    components: {timerset},
    data() {
      return {
        form: {},
        selectionList: [],
        loading: true,
        excelBox: false,
        query: {},
        option: {
          refreshBtn: false,
          searchShow: false,
          tip: false,
          tree: true,
          border: true,
          index: false,
          selection: false,
          editBtn: false,
          delBtn: false,
          menuWidth: 120,
          columnBtn: false,
          addBtn: false,
          column: [
            {
              label: "工作日",
              prop: "workday",
              search: false,
              addDisplay: false,
              editDisplay:false,
              width: 120,
              align: 'center'
            },
            {
              label: "定时定点时间",
              prop: "timeSlots",
              search: false,
              addDisplay: false,
              editDisplay:false,
              align: 'center'
            },
          ]
        },
        data: [],
        workObj: {
          0: '默认',
          1: '星期一',
          2: '星期二',
          3: '星期三',
          4: '星期四',
          5: '星期五',
          6: '星期六',
          7: '星期日',
        },
        workData: [
          {workday: '星期一', timeSlots: '', workdateType: 1, timeSlotList: []},
          {workday: '星期二', timeSlots: '', workdateType: 2, timeSlotList: []},
          {workday: '星期三', timeSlots: '', workdateType: 3, timeSlotList: []},
          {workday: '星期四', timeSlots: '', workdateType: 4, timeSlotList: []},
          {workday: '星期五', timeSlots: '', workdateType: 5, timeSlotList: []},
          {workday: '星期六', timeSlots: '', workdateType: 6, timeSlotList: []},
          {workday: '星期日', timeSlots: '', workdateType: 7, timeSlotList: []},
        ],
        workDataOrigin: [
          {workday: '星期一', timeSlots: '', workdateType: 1},
          {workday: '星期二', timeSlots: '', workdateType: 2},
          {workday: '星期三', timeSlots: '', workdateType: 3},
          {workday: '星期四', timeSlots: '', workdateType: 4},
          {workday: '星期五', timeSlots: '', workdateType: 5},
          {workday: '星期六', timeSlots: '', workdateType: 6},
          {workday: '星期日', timeSlots: '', workdateType: 7},
        ],
        scopeRow: null
      };
    },
    computed: {
      ...mapGetters(["userInfo", "permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.dict_add, false),
          viewBtn: this.vaildData(this.permission.dict_view, false),
          delBtn: this.vaildData(this.permission.dict_delete, false),
          editBtn: this.vaildData(this.permission.dict_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    watch: {
      timerBox(n ,o) {
        if (n) {
          this.workData = this.workDataOrigin
          this.onLoad()
        }
      }
    },
    methods: {
      handleImport(row) {
        if(row && row.workdateType) {
          this.scopeRow = row
        } else {
          this.scopeRow = null
        }
        this.excelBox = true;
      },
      handleAdd(row) {
        this.$refs.crud.value.code = row.code;
        this.$refs.crud.value.parentId = row.id;
        this.$refs.crud.option.column.filter(item => {
          if (item.prop === "code") {
            item.value = row.code;
            item.addDisabled = true;
          }
          if (item.prop === "parentId") {
            item.value = row.id;
            item.addDisabled = true;
          }
        });
        this.$refs.crud.rowAdd();
      },
      rowSave(row, done, loading) {
        row.trueCapacity = parseFloat(row.trueCapacity) >0 ? parseFloat(row.trueCapacity) : 0
        add(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowUpdate(row, index, done, loading) {
        row.trueCapacity = parseFloat(row.trueCapacity) >0 ? parseFloat(row.trueCapacity) : 0
        update(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      beforeOpen(done, type) {
//        if (["edit", "view"].includes(type)) {
//          getDict(this.form.id).then(res => {
//            this.form = res.data;
//          });
//        }
        done();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      refresh() {
        this.excelBox = false
        this.workData = this.workDataOrigin
        this.onLoad()
      },
      allUse() {
        batchExecute({garbageSortDeviceId:this.timerRow.id}).then(() => {
          this.onLoad();
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
        });
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getDetail({garbageSortDeviceId: this.timerRow.id, workdateType: 0}).then(res => {
          this.data = res.data;
          this.loading = false;
          (res.data || []).forEach(item => {
            this.workData.forEach(i => {
              if (item.workdateType === i.workdateType) {
                i.timeSlots = item.timeSlots
                i.timeSlotList = item.timeSlotList
              }
            })
          })
        });
      }
    }
  };
</script>


